package com.qf.controller;

import com.qf.config.RedisLockUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/16 22:12
 * description:
 */
@RestController
public class SecondKillController {

    //商品库存
    private static final Map<String,Long> itemStock=new HashMap<>();
    //商品订单
    private static final Map<String,Long> itemOrder=new HashMap<>();

    static {
        itemStock.put("computer",1000L);
        itemOrder.put("computer",0L);
    }

    @Autowired
    CuratorFramework cf;


    @RequestMapping("/kill")
    public String secondKill(String item) throws InterruptedException {
        //参数1： 客户端对象  参数2： 监听的节点
        InterProcessMutex lock=new InterProcessMutex(cf,"/lock");
        try {

            //获取锁，最大等待时间为2秒
            if(lock.acquire(2, TimeUnit.MINUTES)){
                //1.获取商品信息
                //获取商品剩余数量
                Long count = itemStock.get(item);
                //2.判断商品是否已经卖完
                //2.1如果没有卖完，继续卖
                if(count>0){
                    Thread.sleep(5);
                    itemStock.put(item,count-1);
                    Thread.sleep(5);
                    itemOrder.put(item,itemOrder.get(item)+1);
                    return "商品:"+item+"     "+"剩余:"+itemStock.get(item)+"     "+"已售:"+itemOrder.get(item);
                }else {
                    //2.2如果已经卖完，提示已经卖完
                    return "商品:"+item+"已售罄"+"已售:"+itemOrder.get(item);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放锁资源
            try {
                lock.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "系统异常";
    }


    @Autowired
    RedisLockUtil lockUtil;

    //redis实现分布式锁
    @RequestMapping("/rkill")
    public String rKill(String item) throws InterruptedException {

        if(lockUtil.lock(item,"1",10)){
            //1.获取商品信息
            //获取商品剩余数量
            Long count = itemStock.get(item);
            //2.判断商品是否已经卖完
            //2.1如果没有卖完，继续卖
            if(count>0){
                Thread.sleep(5);
                itemStock.put(item,count-1);
                Thread.sleep(5);
                itemOrder.put(item,itemOrder.get(item)+1);
                //释放锁
                lockUtil.unlock(item);
                return "商品:"+item+"     "+"剩余:"+itemStock.get(item)+"     "+"已售:"+itemOrder.get(item);
            }else {
                lockUtil.unlock(item);
                //2.2如果已经卖完，提示已经卖完
                return "商品:"+item+"已售罄"+"已售:"+itemOrder.get(item);
            }
        }

        return "获取锁失败";



    }
}
